package com.dingwei.superiordiary.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.allen.library.SuperButton;
import com.dingwei.superiordiary.R;
import com.dingwei.superiordiary.adapter.adapter.MyAddressAdapter;
import com.dingwei.superiordiary.bean.MyAddressBean;
import com.dingwei.superiordiary.mvp.presenter.MyAddressPresenter;
import com.dingwei.superiordiary.mvp.view.Contract;
import com.jude.easyrecyclerview.decoration.DividerDecoration;
import com.lihao.baselibrary.base.BaseActivity;
import com.lihao.baselibrary.utils.DisplayUtil;
import com.nikhilpanju.recyclerviewenhanced.RecyclerTouchListener;

import org.slf4j.spi.MDCAdapter;

import java.io.Serializable;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by lihao on 2018/1/11.
 */

public class MyAddressActivity extends BaseActivity implements Contract.MyAddressView {

    MyAddressPresenter myAddressPresenter = new MyAddressPresenter(this);

    @Bind(R.id.img_back)
    ImageView imgBack;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.tv_right)
    TextView tvRight;
    @Bind(R.id.m_recyclerView)
    RecyclerView mRecyclerView;
    @Bind(R.id.btn_add_address)
    SuperButton btnAddAddress;
    @Bind(R.id.no_data_layout)
    RelativeLayout noDataLayout;
    @Bind(R.id.data_layout)
    LinearLayout dataLayout;
    @Bind(R.id.no_net_btn)
    TextView noNetBtn;
    @Bind(R.id.no_net_layout)
    RelativeLayout noNetLayout;

    private MyAddressAdapter adapter;
    private RecyclerTouchListener onTouchListener;


    @Override
    protected int getContentViewId() {
        return R.layout.activity_my_address;
    }

    @Override
    protected void initData() {
        myAddressPresenter.initData();
    }

    protected void initView() {
        tvTitle.setText("管理收货地址");
        LinearLayoutManager layoutManager = new LinearLayoutManager(context);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setNestedScrollingEnabled(false);
        mRecyclerView.addItemDecoration(new DividerDecoration(getResources().getColor(R.color.bg1), DisplayUtil.dip2px(BaseActivity.context, 8)));
        adapter = new MyAddressAdapter(this);
        mRecyclerView.setAdapter(adapter);
    }

    @Override
    protected void initListener() {
        onTouchListener = new RecyclerTouchListener(this, mRecyclerView);
        onTouchListener.setClickable(new RecyclerTouchListener.OnRowClickListener() {
            @Override
            public void onRowClicked(int position) {
//                        showToast("Row " + (position + 1) + " clicked!");
                if (getIntent().hasExtra("type")){
                    if (getIntent().getStringExtra("type").equals("1")){
                        MyAddressBean.DataBean bean = adapter.getItem(position);
                        intent = new Intent();
                        intent.putExtra("address_info", bean);
                        setResult(Activity.RESULT_OK, intent);
                        finish();
                    }
                }

            }

            @Override
            public void onIndependentViewClicked(int independentViewID, int position) {
//                        showToast("Button in row " + (position + 1) + " clicked!");
            }
        })
                .setSwipeOptionViews(R.id.tv_delete)
                .setSwipeable(R.id.rowFG, R.id.rowBG, new RecyclerTouchListener.OnSwipeOptionsClickListener() {
                    @Override
                    public void onSwipeOptionClicked(int viewID, int position) {
                        if (viewID == R.id.tv_delete) {
                            MyAddressBean.DataBean bean = adapter.getItem(position);
                            myAddressPresenter.deleteAddress(bean.id,position);
                        }
                    }
                });
        adapter.editAddressInterfaceListener(new MyAddressAdapter.addressInterFace() {
            @Override
            public void setDefaultAddress(int position) {
                MyAddressBean.DataBean bean = adapter.getItem(position);
                myAddressPresenter.setDefaultAddress(bean.id,position);
            }

            @Override
            public void editAddress(int position) {
                MyAddressBean.DataBean bean = adapter.getItem(position);
                intent = new Intent(BaseActivity.context, AddAddressActivity.class);
                intent.putExtra("bean",  bean);
                startActivity(intent);
            }
        });
    }


    @Override
    protected void onResume() {
        super.onResume();
        mRecyclerView.addOnItemTouchListener(onTouchListener);
        myAddressPresenter.initData();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mRecyclerView.removeOnItemTouchListener(onTouchListener);
    }

    public void showData(String state) {
        if (state.equals("success")) {
            noNetLayout.setVisibility(View.GONE);
            dataLayout.setVisibility(View.VISIBLE);
            if (adapter.getAllData() == null || adapter.getAllData().size() == 0) {
                noDataLayout.setVisibility(View.VISIBLE);
                mRecyclerView.setVisibility(View.GONE);
            } else {
                mRecyclerView.setVisibility(View.VISIBLE);
                noDataLayout.setVisibility(View.GONE);
            }
        } else {
            dataLayout.setVisibility(View.GONE);
            noNetLayout.setVisibility(View.VISIBLE);
            mRecyclerView.setVisibility(View.GONE);
            noDataLayout.setVisibility(View.GONE);
        }
    }


    @Override
    public void setData(MyAddressBean bean) {
        adapter.clear();
        adapter.addAll(bean.data);

        showData("success");
    }

    @Override
    public void onFail() {
        showData("fail");
    }

    @Override
    public void deleteSuccess(int position) {
        adapter.remove(position);
    }

    @Override
    public void setDefaultSuccess(int position) {
//        List<MyAddressBean.DataBean> list = adapter.getAllData();
        for (int i = 0; i < adapter.getAllData().size(); i++) {
            adapter.getAllData().get(i).is_default = "1";
        }
        adapter.getAllData().get(position).is_default = "2";
        adapter.notifyDataSetChanged();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.img_back, R.id.btn_add_address, R.id.no_net_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.btn_add_address:
                intent = new Intent(BaseActivity.context, AddAddressActivity.class);
                startActivity(intent);
                break;
            case R.id.no_net_btn:
                myAddressPresenter.initData();
                break;
        }
    }
}
